package com.lyk.coursearrange.controller;

import com.lyk.coursearrange.common.core.base.code.CodeEnum;
import com.lyk.coursearrange.common.core.base.result.R;
import com.lyk.coursearrange.common.exception.MZException;
import com.lyk.coursearrange.common.log.annotation.Log;
import com.lyk.coursearrange.common.log.enums.OperationType;
import com.lyk.coursearrange.entity.CustomerInformation;
import com.lyk.coursearrange.entity.Job;
import com.lyk.coursearrange.entity.dto.CustomerInformationDto;
import com.lyk.coursearrange.entity.dto.CustomerInformationDtoTwo;
import com.lyk.coursearrange.entity.dto.JobDto;
import com.lyk.coursearrange.service.JobService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.apache.bcel.classfile.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/job")
public class JobController {

    @Autowired
    private JobService jobService;



    /**
     * 查询职级列表
     */
    @ApiOperation(value = "分页查询职级列表", notes = "查询职级列表")
    @PostMapping("joblist")
    @Log(title = "分页查询职级列表", operationType = OperationType.SELECT)
    public R joblist(@RequestBody Map<String, String> param) throws MZException {
        List<Job> list = jobService.joblist(param);
        Long count = jobService.jobcount(param);
        List<JobDto> jobDtos = new ArrayList<>();
        list.forEach(job -> {
            jobDtos.add(JobDto.builder()
                            .jobId(job.getJobId())
                            .jobBasepay(job.getJobBasepay())
                            .jobName(job.getJobName())
                            .createTime(job.getCreateTime())
                            .updateTime(job.getUpdateTime())
                    .build());
        });
        return R.ok()
                .data("list", jobDtos)
                .data("count", count)
                .message("查询职级列表成功");
    }




    /**
     * 添加职级
     */
    @ApiOperation(value = "添加职级", notes = "添加职级")
    @PostMapping(path = "/jobadd")
    @Log(title = "添加职级", operationType = OperationType.INSERT)
    public R jobadd(@RequestBody Job job) throws MZException {
        String job_name = job.getJobName();
        if(jobService.selectjobname(job_name)>0){
            throw new MZException(CodeEnum.JOBNAME);
        }
        boolean a = jobService.save(job);
        if (a) {

            return R.ok().message("添加职级成功");
        } else {
            throw new MZException(CodeEnum.JOBADD);
        }
    }







    /**
     * 查看职级详情
     */
    @ApiOperation(value = "查看职级详情", notes = "查看职级详情")
    @PostMapping(path = "/jobdetails")
    @Log(title = "查看职级详情", operationType = OperationType.SELECT)
    public R jobdetails(@RequestBody Map<String, String> param) throws MZException {
        JobDto jobDto = jobService.jobdetails(param);
        return R.ok()
                .data("details", jobDto)
                .message("查看职级详情成功");
    }







    /**
     * 设置职级信息
     */

    @ApiOperation(value = "设置职级信息", notes = "设置职级信息")
    @PutMapping("jobset")
    @Log(title = "设置职级信息", operationType = OperationType.OTHER)
    public R jobset(@RequestBody JobDto jobDto) throws MZException {
        boolean a = jobService.jobset(jobDto);
        if (a) {
            return R.ok().message("设置职级信息成功!!");
        } else {
            throw new MZException(CodeEnum.JOBSET);
        }
    }






}
